<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>角色管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="角色名称/权限关键字" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="query()" class="dalfBut" icon="el-icon-search">查询</el-button>
                <el-button type="primary" class="butT" @click="newlybuild()" icon="el-icon-circle-plus-outline">新建
                </el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column width="100%" type="index" align="center" label="序号"></el-table-column>
                <el-table-column width="100%" prop="id" label="角色id" align="center"></el-table-column>
                <el-table-column width="100%" prop="name" label="角色名称" align="center"></el-table-column>
                <el-table-column width="100%" prop="keyword" label="角色关键字" align="center"></el-table-column>
                <el-table-column width="100%" prop="description" label="角色说明" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="edit(scope.row)" icon="el-icon-edit">编辑</el-button>
                        <el-button size="mini" type="danger" @click="delete1(scope.row)" icon="el-icon-error">删除</el-button>
                        <el-button size="mini" type="danger" @click="Permissionmenu(scope.row)" icon="el-icon-setting">权限菜单</el-button>
                        <el-button size="mini" type="danger" onclick="javascrtpt:window.location.href='AssignUsers.html'" icon="el-icon-setting">
                            分配用户
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="角色信息" name="first">
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="24">
                                            <el-form-item label="角色关键字" prop="keyword">
                                                <el-input v-model="formData.keyword"
                                                          placeholder="控制器中定义的权限字符，例：ROLE_ADMIN"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色说明" >
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisibleEdit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="角色信息" name="first">
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name" placeholder="系统管理员"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="24">
                                            <el-form-item label="角色关键字" prop="keyword" >
                                                <el-input v-model="formData.keyword"
                                                          placeholder="控制器中定义的权限字符，例：ROLE_ADMIN"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色说明">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!--分配权限菜单标签弹层-->
            <div class="add-form">
                <el-dialog title="分配权限菜单" :visible.sync="AssignPermissions">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="分配权限" name="first">
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色名称">
                                                <el-input v-model="formData.name" placeholder="系统管理员"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="24">
                                            <el-form-item label="角色关键字">
                                                <el-input v-model="formData.keyword"
                                                          placeholder="控制器中定义的权限字符，例：ROLE_ADMIN"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                    <div class="checkScrol">
                                        <table class="datatable">
                                            <thead>
                                            <tr>
                                                <th>选择</th>
                                                <th>ID</th>
                                                <th>权限</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <tr v-for="c in tableData">
                                                <td><input :id="c.id" v-model="jurisdictionIds" type="checkbox" :value="c.id"></td>

                                                <td><label :for="c.id">{{c.id}}</label></td>
                                                <td><label :for="c.id">{{c.name}}</label></td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>

                            </el-tab-pane>
                            <el-tab-pane label="分配菜单" name="second">
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色名称">
                                                <el-input v-model="formData.name"
                                                          placeholder="系统管理员"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="24">
                                            <el-form-item label="角色关键字">
                                                <el-input v-model="formData.keyword"
                                                          placeholder="控制器中定义的权限字符，例：ROLE_ADMIN"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>ID</th>
                                            <th>菜单</th>

                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in Data">
                                            <td><input :id="c.id" v-model="menuIds" type="checkbox" :value="c.id"></td>

                                            <td><label :for="c.id">{{c.id}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="AssignPermissions = false">取消</el-button>
                        <el-button type="primary" @click="idiot()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            jurisdictionIds:[],//权限选择项
            id:null,//弹出权限分配菜单
            nb:null,//删除校验
            menuIds:[],//菜单选择项
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//发送请求获取页面回显参数  权限
            Data: [],//发送请求获取页面回显参数  菜单
            checkitemIds: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisibleEdit: false,//控制编辑窗口显示/隐藏
            AssignPermissions: false,//控制分配权限菜单标签弹层显示/隐藏
            rules: {//校验规则
                name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
                keyword: [{ required: true, message: '关键字不能为空', trigger: 'blur' }],
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            //编辑
            handleEdit() {
                axios.post("/role/edit.do", this.formData).then(res => {
                    this.resetForm();
                    //隐藏窗口
                    this.dialogFormVisibleEdit = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    this.findPage();//分页查询
                })
            },
            //添加
            handleAdd() {
                //提交表单数据
                axios.post("/role/add.do", this.formData).then(res => {
                    this.resetForm();
                    //隐藏窗口
                    this.dialogFormVisible = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    this.findPage();//分页查询
                })
            },
            //分页查询
            findPage() {
                var parms = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                }

                axios.post("/role/findPage.do", parms).then(res => {
                    //后台返回的是PageResult total rows
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                })
            },
            //模糊查询
            query() {
                var parms = {
                    queryString: this.pagination.queryString
                }
                axios.post("/role/query.do", parms).then(res => {
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                })

            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.checkitemIds = [];
                this.activeName = 'first';
            },
            // 弹出添加窗口
            newlybuild() {
                this.resetForm();
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            edit(row) {
                // //弹出编辑窗口
                this.dialogFormVisibleEdit = true;
                // // 并回显角色信息
                axios.get("/role/findById.do?groupId=" + row.id).then(res => {
                    this.formData = res.data.data;//回显角色数据
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                })
            },
            //弹出权限分配菜单
            Permissionmenu(row) {
                //弹出权限分配菜单
                this.AssignPermissions = this;
                this.id = row.id;
                //发送请求获取页面回显参数//角色信息
                axios.get("/role/findById.do?groupId=" + row.id).then(res => {
                    this.formData = res.data.data;//回显角色数据
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                });
                //权限
                axios.post("/role/QueryAuthority.do").then(res => {
                    this.tableData = res.data.data;
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                });
                    //菜单
                    axios.post("/role/InquiryMenu.do").then(res => {
                        this.Data = res.data.data;
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    });
                //权限选择项
                axios.get("/role/jurisdictionIds.do?jurisdictionId=" + row.id).then(res => {
                    this.jurisdictionIds = res.data.data;//回显权限选择项数据
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                });
                //菜单选择项
                axios.get("/role/menuIds.do?menuId=" + row.id).then(res => {
                    this.menuIds = res.data.data;//回显菜单选择项数据
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            delete1(row) {
                //console.log("rrr")
                this.$confirm('此操作将永久删除角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //校验是否有关联
                        axios.get("/role/check.do?id=" + row.id).then(res => {
                            this.nb = res.data.data;
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : 'error'
                            });
                        });
                        if ( this.nb == true) {
                    //发送删除请求
                    axios.get("/role/deleteById.do?id=" + row.id).then(res => {
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success' : 'error'
                        });
                        this.findPage();//分页查询
                    })}else {
                        this.$message.error("有关联无法删除")
                        this.nb = null;
                        }
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //分配权限与菜单
            idiot(){
                axios.post("/role/jurisdictionId.do?id="+this.id,this.jurisdictionIds).then(res => {
                    this.AssignPermissions = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    this.findPage();//分页查询
                });
                axios.post("/role/menuId.do?id="+this.id,this.menuIds).then(res => {
                    this.AssignPermissions = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    this.findPage();//分页查询
                })
            },
        },

    })
</script>
</html>
